package com.kcsj.controller.user;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.kcsj.domain.user.Resource;
import com.kcsj.service.user.ResourceService;
import com.kcsj.util.JSONUtil;
import com.kcsj.util.MessageHelper;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;

/**
 * @author 胡庆阳
 * 29th Nov. 2020
 */
@Slf4j
@WebServlet("/resource")
public class ResourceController extends HttpServlet {
    //添加资源
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //根据request对象，获得代表参数的JSON字串
        String json = JSONUtil.getJSON(request);
        try {
            //判断是否为空字符串
            if(!json.equals("")){
                //将JSON字串解析为student对象
                Resource resource = JSON.parseObject(json, Resource.class);
                ResourceService.getInstance().add(resource);
                JSONObject message = MessageHelper.addMessage("增加成功");
                response.getWriter().println(message);
            }else {
                JSONObject message = MessageHelper.addMessage("未收到请求信息！");
                response.getWriter().println(message);
            }
        }catch (SQLException e){
            e.printStackTrace();
            log.error(e.getMessage());
            JSONObject message = MessageHelper.dateBaseError();
            response.getWriter().println(message);
        }catch(Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            JSONObject message = MessageHelper.netWordError();
            response.getWriter().println(message);
        }
    }
    //修改资源
    @Override
    protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String json = JSONUtil.getJSON(request);
        try {
            if(json.equals("")){
                log.info("未收到请求信息！");
                JSONObject message = MessageHelper.addMessage("未收到请求信息！");
                response.getWriter().println(message);
            }else {
                //将JSON字串解析为Resource对象
                Resource resource = JSON.parseObject(json,Resource.class);
                //到数据库表修改Resource对象对应的记录
                ResourceService.getInstance().update(resource);
                log.info("resource{}修改成功",resource.getId());
                JSONObject message = MessageHelper.addMessage("修改成功");
                response.getWriter().println(message);
            }
        }catch (SQLException e){
            log.error(e.getMessage());
            JSONObject message = MessageHelper.dateBaseError();
            response.getWriter().println(message);
        }catch(Exception e){
            log.error(e.getMessage());
            JSONObject message = MessageHelper.netWordError();
            response.getWriter().println(message);
        }
    }

    //删除资源
    @Override
    protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");

        try {
            if (!id.equals("")) {
                //到数据库表中删除对应的学位
                ResourceService.getInstance().delete(Integer.parseInt(id));
                JSONObject message = MessageHelper.addMessage("删除成功");
                log.info("resource{}删除成功",id);
                response.getWriter().println(message);
            }else {
                JSONObject message = MessageHelper.addMessage("未收到请求信息");
                response.getWriter().println(message);
            }
        }catch (SQLException e){
            log.error(e.getMessage());
            JSONObject message = MessageHelper.dateBaseError();
            response.getWriter().println(message);
        }catch(Exception e){
            log.error(e.getMessage());
            JSONObject message = MessageHelper.netWordError();
            response.getWriter().println(message);
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            this.responseResources(response);
        }catch (SQLException e){
            log.error(e.getMessage());
            JSONObject message = MessageHelper.dateBaseError();
            response.getWriter().println(message);
        }catch(Exception e){
            log.error(e.getMessage());
            JSONObject message = MessageHelper.netWordError();
            response.getWriter().println(message);
        }
    }

    //按查询条件响应资源对象
    private void responseResources(HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        //获得所有资源
        Collection<Resource> students = ResourceService.getInstance().findAll();
        String students_json = JSON.toJSONString(students, SerializerFeature.DisableCircularReferenceDetect);
        JSONObject data = MessageHelper.addData(students_json);
        //响应json到前端
        response.getWriter().println(data);
    }
}
